<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Think\Page;

/**
 * 文章控制器
 * @author jry <598821125@qq.com>
 */
class KolController extends AdminController
{
    /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
    public $tag1 = array('1'=>'美食','2'=>'艺术','3'=>'手工','4'=>'形体','5'=>'心理','6'=>'摄影','7'=>'礼仪');
    public $tag2 = array('1'=>'小仙女','2'=>'强迫症','3'=>'天生吃货','4'=>'女神经','5'=>'暖男','6'=>'脑洞大开','7'=>'颜值爆表','8'=>'天马行空','9'=>'唯美','10'=>'时尚达人');
    public $tag3 = array('1'=>'母婴','2'=>'亲子','3'=>'时尚','4'=>'智能家居','5'=>'金融','6'=>'珠宝','7'=>'奢侈品','8'=>'房地产');
    public function index()
    {
        $uid = session('user_auth.uid');
        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group'); 
        // 搜索
        $keyword         = I('keyword', '', 'string');
        $condition       = array('like', '%' . $keyword . '%');
        $map['id|name'] = array($condition, $condition, '_multi' => true);
        $p             = !empty($_GET["p"]) ? $_GET['p'] : 1;
        
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        
        $post_object   = D('Admin/Kol');
        $data_list     = $post_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new Page(
            $post_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );
        
        // 使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->addTopButton('addnew', array('href' => U('add'))) // 添加新增按钮
            ->setSearch('请输入ID/名字', U('index'))
            ->addRightButton('edit')// 添加编辑按钮
            ->addRightButton('forbid');
        $builder->setMetaTitle('KOL列表') // 设置页面标题
            ->addTableColumn('id', 'ID')
            //->addTableColumn('postname', '活动名')
            ->addTableColumn('name', '名字')
            //->addTableColumn('brand_name', '品牌名')
            //->addTableColumn('weibo', '微博粉丝数')
            //->addTableColumn('wechat', '微信公众号粉丝数')
            //->addTableColumn('wechatgroup', '自运营微信群数量')
            ->addTableColumn('create_time', '创建时间', 'time')
            ->addTableColumn('update_time', '更新时间', 'time')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list) // 数据列表
            ->setTableDataPage($page->show()) // 数据列表分页
            
            //->addRightButton('delete') // 添加删除按钮
            ->display();
    }

    /**
     * 新增文档
     * @author jry <598821125@qq.com>
     */
    public function add()
    {
        if (IS_POST) {
            $post_object = D('Admin/Kol');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->add();
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增KOL') // 设置页面标题
                ->setPostUrl(U('add')) // 设置表单提交地址
                
                //->addFormItem('post_id','select','活动名','活动名',select_list_as_tree('Admin/Post'))
                ->addFormItem('introimg','subpicture','介绍页头图','介绍页头图')
                ->addFormItem('kvimg','subpicture','邀请函内头像','邀请函内头像','1/1')
                ->addFormItem('name','text','姓名','润润')
				->addFormItem('nametag','text','个人标签','例如：时尚博主，美食达人，国际XX认证导师，资深XX师')
                ->addFormItem('stars', 'select', '星级', '星级',array('1' => '一星', '2' => '二星', '3' => '三星', '4' => '四星', '5' => '五星'))
                //->addFormItem('tag1','checkbox','平台标签','平台标签',$this->tag1)
                //->addFormItem('tag2','checkbox','个性标签','个性标签',$this->tag2)
                //->addFormItem('tag3','text','自定义标签','自定义标签')
                ->addFormItem('domain','checkbox','领域','领域',$this->tag3)
                ->addFormItem('area','text','所在地','所在地')
                ->addFormItem('intro','textarea','简介','简介')
                //->addFormItem('works','kindeditor','作品','作品')
                //->addFormItem('classimg','pictures','曾开课程','曾开课程')
                ->addFormItem('showimg','pictures','技能SHOW','技能SHOW')
                //->addFormItem('weibo','text','微博粉丝数','微博粉丝数')
                //->addFormItem('wechat','text','微信公众号粉丝数','微信公众号粉丝数')
                //->addFormItem('wechatgroup','text','自运营微信群数量','自运营微信群数量')
                //->addFormItem('num', 'select', '场次(1000元/人次)', '场次',array('5' => 5, '10' => 10,'15'=>15,'20'=>20),'','id=num')
                ->display();
        }
    }

    /**
     * 编辑文章
     * @author jry <598821125@qq.com>
     */
    public function edit($id)
    {
        if (IS_POST) {
            $post_object = D('Admin/Kol');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->save();
                if ($id !== false) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            // 使用FormBuilder快速建立表单页面。
            $map['status'] = array('egt', '0'); // 禁用和正常状态

            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑合同') // 设置页面标题
                ->setPostUrl(U('edit')) // 设置表单提交地址
                ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('introimg','subpicture','介绍页头图','介绍页头图')
                ->addFormItem('kvimg','subpicture','邀请函内头像','邀请函内头像','1/1')
                ->addFormItem('name','text','姓名','润润')
				->addFormItem('nametag','text','个人标签','例如：时尚博主，美食达人，国际XX认证导师，资深XX师')
                ->addFormItem('stars', 'select', '星级', '星级',array('1' => '一星', '2' => '二星', '3' => '三星', '4' => '四星', '5' => '五星'))
                //->addFormItem('tag1','checkbox','平台标签','平台标签',$this->tag1)
                //->addFormItem('tag2','checkbox','个性标签','个性标签',$this->tag2)
                //->addFormItem('tag3','text','自定义标签','自定义标签')
                ->addFormItem('domain','checkbox','领域','领域',$this->tag3)
                ->addFormItem('area','text','所在地','所在地')
                ->addFormItem('intro','textarea','简介','简介')
                //->addFormItem('works','kindeditor','作品','作品')
                //->addFormItem('classimg','pictures','曾开课程','曾开课程')
                ->addFormItem('showimg','pictures','技能SHOW','技能SHOW')
                //->addFormItem('weibo','text','微博粉丝数','微博粉丝数')
                //->addFormItem('wechat','text','微信公众号粉丝数','微信公众号粉丝数')
                //->addFormItem('wechatgroup','text','自运营微信群数量','自运营微信群数量')
                ->setFormData(D('Admin/Kol')->find($id))
                ->display();
        }
    }

    // 将树形列表转换为树
    public function list_as_tree($list, $extra = null, $key = 'id', $title_field = 'title')
    {
        //转换成树状列表(非严格模式)
        $tree = new \Util\Tree();
        $list = $tree->array2tree($list, $title_field, 'id', 'pid', 0, false);

        if ($extra) {
            $result[0] = $extra;
        }

        //转换成一维数组
        foreach ($list as $val) {
            $result[$val[$key]] = $val['title_show'];
        }
        return $result;
    }

    /**
     * 设置一条或者多条数据的状态
     * @author jry <598821125@qq.com>
     */
    public function setStatus($model = CONTROLLER_NAME, $script = false)
    {
        $ids = I('request.ids');
        parent::setStatus($model);
    }
}
